package com.hk.player.utils;

import android.content.Context;
import android.graphics.Point;
import android.view.Gravity;
import android.view.WindowManager;

import com.zhoug.common.interfaces.Callback2;
import com.zhoug.common.network.NetworkUtils;
import com.zhoug.common.utils.AppUtils;
import com.zhoug.zmui.dialog.ZMUIButtonDialog;

import androidx.annotation.NonNull;


/**
 * @Author 35574
 * @Date 2020/5/4
 * @Description
 */
public class AutoPlayUtils {
    //移动网络时是否自动播放
    private static Boolean autoPlay = null;

    /**
     * 移动网络下是否自动播放
     *
     * @return
     */
    public static Boolean isAutoPlay( Context appContext) {
        //移动网络
        if(NetworkUtils.isMobileAvailable(appContext)){
            return autoPlay!=null && autoPlay;
        }
        //非移动网络
        return true;
    }

    private static void setAutoPlayWithMobileNetwork(boolean autoPlay) {
        AutoPlayUtils.autoPlay = autoPlay;
    }



    /**
     * 移动网络提示
     * @param context
     */
    public static void showMobileNetworkHint(Context context, @NonNull Callback2<Boolean> callback2) {
        if (NetworkUtils.isMobileAvailable(context)) {
            if (autoPlay == null) {
                ZMUIButtonDialog buttonDialog = new ZMUIButtonDialog(context);
                Point screenSize = AppUtils.getScreenSize();
                buttonDialog.setTitle("播放提示");
                buttonDialog.setMessage("当前为移动网络是否继续播放?");
                buttonDialog.setCancelable(false);
                buttonDialog.setCanceledOnTouchOutside(false);

                buttonDialog.setPositiveListener("播放", v -> {
                    buttonDialog.cancel();
                    setAutoPlayWithMobileNetwork(true);
                    callback2.onResult(true);

                });
                buttonDialog.setNegativeListener("取消", v -> {
                    buttonDialog.cancel();
                    setAutoPlayWithMobileNetwork(false);
                    callback2.onResult(false);
                });
                buttonDialog.show();
                buttonDialog.setAttributes(screenSize.x * 2 / 3, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

            } else {
                if (autoPlay) {
                    callback2.onResult(true);
                } else {
                    callback2.onResult(false);
                }
            }
        } else {
            callback2.onResult(true);
        }


    }


}
